package MazeGenProject;

import java.io.Serializable;

public class JoinRoom implements Serializable
{
	private int[] rooms; //Array of rooms

	public JoinRoom(int elem) 
	{
		rooms = new int[elem];
		for (int i = 0; i < rooms.length; i++) 
		{
			rooms[i] = -1;
		}
	}

	//Find Room
	public int find(int r) 
	{
		if (rooms[r] < 0) 
		{
			return r;
		} 
		else 
		{
			return rooms[r] = find(rooms[r]);
		}
	}

	//Merge rooms
	public void unionRooms(int roomA, int roomB) 
	{
		if (rooms[roomB] < rooms[roomA]) 
		{
			rooms[roomA] = roomB;
		} 
		else 
		{
			if (rooms[roomA] == rooms[roomB]) 
			{
				rooms[roomA]--;
			}
			rooms[roomB] = roomA;
		}
	}

}